<?php
import( 'de.ceus-media.framework.neon.DefinitionView' );
import( 'classes.models.RightObject' );
import( 'classes.models.RightAction' );
import( 'classes.models.RightObjectAction' );
/**
 *	Views of Roles.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionView
 *	@uses			RightObject
 *	@uses			RightAction
 *	@uses			RightObjectAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
/**
 *	Views of Roles.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionView
 *	@uses			RightObject
 *	@uses			RightAction
 *	@uses			RightObjectAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 *	@todo			Language Support of Messages
 *	@todo			Code Documentation
 */
class RoleViews extends CMF_Krypton_DefinitionView
{
	/**
	 *	Builds Content Views.
	 *	@access		public
	 *	@return		void
	 */
	public function buildContent()
	{
		$request	= $this->ref->get( "request" );
		$auth	= $this->ref->get( "auth" );

		$content	= "";
		if( $role_id	= $request->get( 'role_id' ) )
		{
			if( $auth->hasRight( 'role', 'edit' ) )
				$content	.= $this->buildFieldEdit();
			if( $auth->hasRight( 'right', 'list' ) )
				$content	.= $this->buildFieldRightField();
		}
		else
		{
			if( $auth->hasRight( 'role', 'list' ) )
				$content	= $this->buildFieldList();
			if( $auth->hasRight( 'role', 'add' ) )
				$content	.= $this->buildFieldAdd();
		}
		return $content;
	}

	/**
	 *	Builds Control View from Wiki File.
	 *	@access		public
	 *	@return		string
	 */
	public function buildControl()
	{
		return $this->loadContent( "role_control.wiki" );
	}
	
	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldAdd()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$words	= $this->words['role']['add'];

		$ui	= $this->buildForm( 'role', 'addRole', 'role', 'add' );

//		$input_rolename	= $this->html->Input( 'add_rolename',		$request->get( 'add_rolename' ), 'in' );
//		$input_description	= $this->html->TextArea( 'add_description',	$request->get( 'add_description' ), 'xs' );
		$button_add		= $this->html->Button( 'addRole', $words['button_add'] );

//		$ui['label_rolename']	= $this->html->Label( 'add_rolename',	$words['add_rolename'] );
//		$ui['label_description']	= $this->html->Label( 'add_description', $words['add_description'] );

//		$ui['field_rolename']	= $this->html->Field( 'add_rolename',		$input_rolename );
//		$ui['field_description']	= $this->html->Field( 'add_description',	$input_description );
		$ui['field_button']		= $this->html->Field( '', $button_add );
		
		$ui['form']		= $this->html->Form( 'addRole', "?link=".$request->get( 'link' ) );
		$ui['colgroup']		= $this->html->ColumnGroup( "30%", "70%" );
		$ui['caption']		= $this->html->TableCaption( $words['caption_add'] );
		$ui['heading']		= $words['heading_add'];
		
		return $this->loadTemplate( "role.add", $ui );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldEdit()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$words	= $this->words['role']['edit'];

		if( $role_id = $request->get( 'role_id' ) )
		{
			$role	= new Role( $role_id );
			$data	= $role->getData();

			$ui	= $this->buildForm( 'role', 'editRole', 'role', 'edit', $data );

//			$input_rolename	= $this->html->Input( 'edit_rolename',		$data['rolename'], 'in' );
//			$input_description	= $this->html->TextArea( 'edit_description',	$data['description'], 'xs' );
			$button_edit		= $this->html->Button( 'editRole',			$words['button_edit'] );
			$button_remove	= $this->html->Button( 'removeRole',		$words['button_remove'], 'buthot', $words['button_remove_confirm'] );

//			$ui['label_rolename']	= $this->html->Label( 'edit_rolename',		$words['edit_rolename'] );
//			$ui['label_description']	= $this->html->Label( 'edit_description',	$words['edit_description'] );

//			$ui['field_rolename']	= $this->html->Field( 'edit_rolename',		$input_rolename );
//			$ui['field_description']	= $this->html->Field( 'edit_description',	$input_description );
			$ui['field_buttons']		= $this->html->Field( '', $button_edit.$button_remove );

			$ui['form']		= $this->html->Form( 'editRole', "?link=".$request->get( 'link' )."&role_id=".$request->get( 'role_id' ) );
			$ui['colgroup']		= $this->html->ColumnGroup( "30%", "70%" );
			$ui['caption']		= $this->html->TableCaption( $words['caption_edit'] );
			$ui['heading']		= $words['heading_edit'];
		
			return $this->loadTemplate( "role.edit", $ui );
		}
		else
			$this->messenger->noteError( $words['msg_error1'] );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldList()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$auth	= $this->ref->get( 'auth' );
		$words	= $this->words['role']['list'];

		$editor	= $auth->hasRight( 'role', 'edit' );

		$list		= array();
		$role	= new Role();
		$roles	= $role->getAllData();
		$i = 0;
		foreach( $roles as $role )
		{
			$style	= ++$i % 2 ? 'list1' : 'list2';
			$label_desc	= $this->html->Label( '', $role['description'] );
			if( $editor )
			{
				$link			= $this->html->Link( "?link=".$request->get( 'link' )."&role_id=".$role['role_id'], $role['rolename'] );
				$label_link	= $this->html->Label( '', $link );
			}
			else
				$label_link	= $this->html->Label( '', $role['rolename'] );
			$list[]	= "<tr class='".$style."'>".$label_link.$label_desc."</tr>";
		}
		$ui['roles']	= implode( "\n\t", $list);

		$heads	= array(
			$words['head_rolename'],
			$words['head_description'],
		);
		$ui['heads']		= $this->html->TableHeads( $heads );
		$ui['colgroup']	= $this->html->ColumnGroup( "30%", "70%" );
		$ui['caption']		= $this->html->TableCaption( $words['caption_list'] );
		$ui['heading']		= $words['heading_list'];

		return $this->loadTemplate( "role.list", $ui );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldRightField()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$words	= $this->words['role']['rights'];

		if( $role_id	= $request->get( 'role_id' ) )
		{
			$ui['colgroup']	= "";
			$ui['field_button']	= "";
			$object	=  new RightObject();
			$action	=  new RightAction();
			$objects	= $object->getObjects();
			$actions	= $action->getActions();
			if( count( $objects ) )
			{
				if( count( $actions ) )
				{
					$roleright	= new RoleRight();
					$relation	= new RightObjectAction();
					$i = 0;
					foreach( $objects as $object_id => $object )
					{
						$object	= new RightObject( $object_id );
						$object	= $object->getData( TRUE );
						$object_rights	= array();
						foreach( $actions as $action_id => $action )
						{
							if( $relation->isObjectActionByID( $object_id, $action_id ) )
							{
								$value	= $roleright->hasRightByID( $role_id, $object_id, $action_id );
								$checked	= $value ? " checked='checked'" : "";
								$input	= "<input name='right[".$object_id."][".$action_id."]' type='checkbox'".$checked.">";
							}
							else
							{
								$input	= $words['no_relation'];
							}
							$object_rights[]	= $this->html->Field( '', $input );
						}
						$style	= ++$i % 2 ? 'list1' : 'list2';
						if( $object['description'] )
							$object['title']	= $this->html->Acronym( $object['title'], $object['description'] );
						$link		= $this->html->Link( "?link=right_object", $object['title'] );
						$label	= $this->html->Label( '', $link );
						$rights[]	= "<tr class='".$style."'>".$label.implode( "", $object_rights )."</tr>";
					}
					$ui['rights']	= implode( "\n\t", $rights );	
			
					$input_button	= $this->html->Button( 'saveRights', $words['button'] );
					$ui['field_button']	= $this->html->Field( '', $input_button, '', '', count( $actions ) );
			
					$cols		= floor( 80 / count( $actions ) );
					$cols		= implode( ", ", array_fill( 0, count( $actions ), "'".$cols."%'" ) );	
					$ui['colgroup']	= eval( "return $"."this->html->ColumnGroup( '20%', ".$cols." );" );
				}
				else
					$ui['rights']	= "<tr><td>".$words['no_actions']."</td></tr>";
			}
			else
				$ui['rights']	= "<tr><td>".$words['no_objects']."</td></tr>";
			$heads	= array( $words['right_object'] );
			foreach( $actions as $action_id => $action )
			{
				$action	= new RightAction( $action_id );
				$action	= $action->getData( TRUE );
				if( $action['description'] )
					$action['title']	= $this->html->Acronym( $action['title'], $action['description'] );
				$heads[]	= $action['title'];
			}
			$ui['heads']		= $this->html->TableHeads( $heads );
			$ui['caption']		= $this->html->TableCaption( $words['caption_rights'] );
			$ui['form']		= $this->html->Form( 'addRight', "?link=".$request->get( 'link' )."&role_id=".$request->get( 'role_id' ) );
			$ui['heading']		= $words['heading_rights'];

			return $this->loadTemplate( "role.rights", $ui );
		}
		else
			$this->messenger->noteError( $words['msg_error1'] );
	}
}
?>
